package org.sdau.rotten.library.service.Impl;

import cn.hutool.core.convert.Convert;

import cn.hutool.crypto.SecureUtil;
import org.sdau.rotten.library.entity.Appointment;
import org.sdau.rotten.library.entity.User;
import org.sdau.rotten.library.service.UserService;
import org.sdau.rotten.library.dao.AppointmentDAO;
import org.sdau.rotten.library.dao.UserDAO;
import org.sdau.rotten.library.exception.NoNumberException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service("userServiceImpl")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDAO userDao;
    @Autowired
    private AppointmentDAO appointmentDao;

    @Override
    public boolean register(User user) {
        boolean flag = userDao.register(
                user.getId(),
                SecureUtil.md5(user.getPassword()),
                user.getEmail(),
                user.getAuthority()
                );

        return flag;
    }

    @Override
    public User isExist(String username, String password) {
        User user = userDao.isExist(Convert.toInt(username),password);
        return user;
    }

    @Override
    public List<Appointment> myAppoint(String studentId) {
        List<Appointment> appointment = appointmentDao.queryByStudentWithBook(Convert.toLong(studentId));
        return appointment;
    }

    @Override
    public List<User> search(String userId) {
        System.out.println("查询了"+'%'+userId+'%');
        List<User> result = userDao.search('%'+userId+'%');
        return  result;
    }

    @Override
    public boolean insertUser(int userId, String password, String authority) {
        System.out.println("插入了人员");
        password = SecureUtil.md5(password);//md5加密一次
        int back = userDao.insert(userId,password,authority);
        if(back>0)
            return true;
        else
            throw new NoNumberException(121,"插入用户失败");
    }


}
